// JavaScript Document
var player = null;
var onlyOver = false;
$(function () {
    resizeBoard();
    $(window).bind("resize", resizeBoard);
    bindTab("#newApply .tabButtons", "tabButton", "#newApply .tabContents");
    bindTab("#rjss .tabButtons", "tabButton", "#rjss .tabContents");
    bindTab("#pkrk .tabButtons", "tabButton", "#pkrk .tabContents");
    bindTab("#dbz .tabButtons", "tabButton", "#dbz .tabContents");
    bindTab("#mb .tabButtons", "tabButton", "#mb .tabContents");
    loadListPlayer();
    bindMoreColume();
    player = setInterval(function () {
        loadListPlayer();
    }, 5000);
    bindBoardEvent();
    queryCount();
    bindShowDetail();
    bindOverRows();
    $("#refresh").bind("click", function () {
        queryCount()
    });
});

function resizeBoard() {

    var baseWidth = ($("#center").width() - 40) * 0.3;
    height = baseWidth * 220 / 500;


    $(".infoBox").height(height + "px");
    $(".listBox").height((height * 3 + 44) + "px");
    $(".infoBox").css("font-size", height / 10 + "px");
    $(".listBox").css("font-size", height / 10 + "px");
    $("h1").css("font-size", height / 9 + "px");
    $("h2").css("font-size", height / 10 + "px");
    $("h3").css("font-size", height / 15 + "px");
    $("h4").css("font-size", height / 5.5 + "px");
    $(".barBox").css("font-size", height / 10 + "px");
    $("#top").css("font-size", height / 7.5 + "px");
}

function bindBoardEvent() {
    $(".infoBox").bind("click", function (e) {
        var index = 0;
        if ($(e.target).hasClass("infoBox"))
            index = $(e.target).attr("index");
        else index = $(e.target).parents(".infoBox").attr("index");
        showList = index * 1;
        loadListPlayer();
        if (player != null)
            clearInterval(player);
        player = null;
    });

    $(".infoBox").bind("mouseleave", function (e) {
        var index = 0;
        if ($(e.target).hasClass("infoBox"))
            index = $(e.target).attr("index");
        else index = $(e.target).parents(".infoBox").attr("index");
        if (player == null)
            player = setInterval(function () {
                loadListPlayer();
            }, 5000);
    });
    $("#apply").bind("click", function (e) {
        showList = 0;
        loadListPlayer();
        if (player != null)
            clearInterval(player);
        player = null;
    });
    $("#apply").bind("mouseleave", function (e) {
        if (player == null)
            player = setInterval(function () {
                loadListPlayer();
            }, 5000);
    });

    $("#right").bind("mouseover", function (e) {
        if (player != null)
            clearInterval(player);
        player = null;
    });
    $("#right").bind("mouseleave", function (e) {
        if (player == null)
            player = setInterval(function () {
                loadListPlayer();
            }, 5000);
    });
}

function bindTab(buttonGroup, buttomClass, contentGroup) {
    $(buttonGroup).children().bind("click", function (e) {
        var index = 0;
        if ($(e.target).hasClass(buttomClass))
            index = $(e.target).index();
        else index = $(e.target).parents(buttomClass).index();

        $(contentGroup).find(".active").removeClass("active");
        $(buttonGroup).find(".active").removeClass("active");
        $(contentGroup).children().eq(index).addClass("active");
        $(buttonGroup).children().eq(index).addClass("active");
        e.stopPropagation();
    })
    $(buttonGroup).children().eq(0).addClass("active");
    $(contentGroup).children().eq(0).addClass("active");
}

var showList = 0;

function loadListPlayer() {
    var pages = $(".pages").children();
    pages.css({"z-index": "0", "opacity": "0"});
    var f = showList;
    var s = f + 1
    if (s == pages.length)
        s = 0;
    var t = s + 1;
    if (t == pages.length)
        t = 0;
    pages.eq(f).css({"z-index": "4", "top": "0px", "left": "0px", "right": "0px", "opacity": "1"});
    pages.eq(s).css({"z-index": "3", "top": "-0.5em", "left": "0.5em", "right": "0.5em", "opacity": "0.5"});
    pages.eq(t).css({"z-index": "2", "top": "-1em", "left": "1em", "right": "1em", "opacity": "0.5"});
    showList++;
    if (showList == pages.length)
        showList = 0;
}

function bindMoreColume() {
    $(".max").bind("click", function (e) {
        if ($(".max").hasClass("am-icon-eye")) {
            $(".max").removeClass("am-icon-eye").addClass("am-icon-eye-slash");
            $(".max").html(" 隐藏列");
            var box = $(e.target).parents(".listBox");
            $("#left").css("flex", "0.2");
            $("#left .infoTips").hide();
            $("#left .infoMsg").css({"font-size": "1em", "margin-top": "calc( 2em - 1px)"});
            $(".listBox .list .tabContent .hide").css("display", "table-cell");
        } else {
            $(".max").removeClass("am-icon-eye-slash").addClass("am-icon-eye");
            $(".max").html(" 展开列");
            $("#left").css("flex", "1.5");
            $("#left .infoTips").show();
            $("#left .infoMsg").css({"font-size": "1.6em", "margin-top": "calc( 1.25em - 1px)"});
            $(".listBox .list .tabContent .hide").css("display", "none");

        }


    });

}

function bindOverRows() {
    $(".over").bind("click", function (e) {
        var box = $(e.target).parents(".listBox");
        if ($(".over").hasClass("am-icon-list")) {
            $(".over").removeClass("am-icon-list").addClass("am-icon-warning");
            $(".over").html(" 显示超限");
            onlyOver = false;
        } else {
            $(".over").removeClass("am-icon-warning").addClass("am-icon-list");
            $(".over").html(" 显示全部");
            onlyOver = true;
        }
        queryCount();
    });

}

function queryCount() {

    checkCount();
    // rjssCount();
    pkrkCount();
    zeCount();
    // dbzCount();
    // mbCount();
    var now = new Date();
    $("#refreshTime").html("更新时间：" + now.getHours() + "时" + now.getMinutes() + "分" + now.getSeconds() + "秒");
}

// setInterval(queryCount, 60 * 1000);

function bindShowDetail() {
    $(".shade").hide();
    $(".listWindow").hide();
    $(".detailWindow").hide();
    bindClose();
    $(".shade").bind("click", function (e) {
        if ($(e.target).hasClass("shade"))
            hideShade();
    });
    $("#newApply tr").bind("click", function () {
        showDetail();
    });
}

function showShade() {
    $("body").css("overflow", "hidden");
    $(".shade").fadeIn(300);
}

function hideShade() {
    $("body").css("overflow", "auto");
    $(".shade").fadeOut(300);
    hideDetail();
}

function showDetail(buttons) {
    showShade();
    $(".detailWindow").show();
    if ($(".detailWindow").hasClass("TA"))
        $(".detailWindow").css({"width": "84%", "left": "calc( 8% )", "top": "6%", "height": "85%", "opacity": "1"});
    $(".detailWindow .detailFoot .button").remove();
    $(".detailWindow .detailFoot .info").html("");
    $(buttons).each(function (index, item) {
        var button = $("<div class='button'>" + item.text + "</div>");
        button.bind("click", item.click);
        $(".detailWindow .detailFoot").append(button);
    })
}

function hideDetail() {
    $(".detailWindow").css({"width": "80%", "left": "calc( 10% )", "top": "10%", "height": "80%", "opacity": "0"});
    setTimeout(function () {
        $(".detailWindow").hide();
    }, 300)
}

function bindClose() {
    var exit = $(".exit");
    exit.bind("click", function (e) {
        hideShade();
        hideDetail();
    })
    var close = $(".close");
    close.bind("click", function (e) {
        hideShade();
        hideDetail();
    })
}